package com.xunsiya.service.module.device.impl;

import com.xunsiya.module.device.dao.DeviceMapper;
import com.xunsiya.module.device.dao.VideoStreamCompanyMapper;
import com.xunsiya.module.device.dao.VideoStreamDeviceDetailMapper;
import com.xunsiya.module.device.vo.Device;
import com.xunsiya.module.device.vo.VideoStreamCompany;
import com.xunsiya.module.device.vo.VideoStreamDeviceDetail;
import com.xunsiya.service.module.device.VideoStreamCompanyService;
import com.xunsiya.tools.common.msg.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 视频传输和企业关联Service
 *
 * @version 1.0
 * @author: xml
 * @date: 2018/11/8
 */
@Service
public class VideoStreamCompanyServiceImpl implements VideoStreamCompanyService {

    @Autowired
    private VideoStreamCompanyMapper videoStreamCompanyMapper;

    @Autowired
    private VideoStreamDeviceDetailMapper videoStreamDeviceDetailMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 通过紫万账号查询分辨率和时长
     *
     * @param zwanNumber 紫万账号
     * @return
     */

    @Override
    public Map<String, Object> queryByZwan(String zwanNumber) {
        //得到视频传输和企业关联结果
        VideoStreamCompany vsc = videoStreamCompanyMapper.queryByZwan(zwanNumber);
        //得到通话记录结果(单位：秒)
        Integer spendTime = videoStreamDeviceDetailMapper.queryByZwan(zwanNumber);
        // 如果没有通话时长记录,直接设为0
        if (spendTime == null) {
            spendTime = 0;
        }
        Map<String, Object> resultMap = new HashMap<>(2);
        // 初始化分钟变量 60秒
        double minute = 60d;
        if (vsc != null) {
            //得到分辨率
            String resolution = vsc.getResolution();
            //得到总时长(单位：小时)
            //Double totalDuration = vsc.getTotalDuration();
            //视频通话总时长暂时不控制(迭代三),初始化总时长999小时
            Double totalDuration=999d;
            //计算剩余时长(单位：分钟)
            BigDecimal bd = new BigDecimal(minute * totalDuration - spendTime / minute);
            bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
            resultMap.put("resolution", resolution);
            resultMap.put("remainderMinutes", bd.doubleValue());
        }
        return resultMap;
    }

    /**
     * 保存视频传输通话记录
     *
     * @param vsdd 设备通话记录
     * @return
     */
    @Override
    public Msg saveRecord(VideoStreamDeviceDetail vsdd) {
        // 得到视频传输和企业关联结果
        VideoStreamCompany vsc = videoStreamCompanyMapper.queryByZwan(vsdd.getSource());
        // 得到通话记录总时长结果(单位：秒)
        Integer spendTime = videoStreamDeviceDetailMapper.queryByZwan(vsdd.getSource());
        // 如果没有通话时长记录,直接设为0
        if (spendTime == null) {
            spendTime = 0;
        }
        // 初始化变量 1小时
        int second = 3600;
        if (vsc != null) {
            //得到总时长
            //Double totalDuration = vsc.getTotalDuration();
            //Double totalDuration = vsc.getTotalDuration();
            //视频通话总时长暂时不控制(迭代三),初始化总时长999小时
            Double totalDuration=999d;
            //判断通话时长是否大于剩余总时长
            if ((second * totalDuration - spendTime) < vsdd.getSpendTime()) {
                return new Msg(false, "剩余总时长" + (second * totalDuration-spendTime) + "秒");
            }
        }
        //通过紫万账号查询设备
        Device device = deviceMapper.findByZwan(vsdd.getSource());
        if (device != null && vsdd != null) {
            //得到企业编号
            Integer companyId = device.getCompanyId();
            //创建者 默认值-1
            vsdd.setCreateBy(-1);
            //创建时间
            vsdd.setCreateTime(new Date());
            //状态可用
            vsdd.setStatus("1");
            //企业编号
            vsdd.setCompanyId(companyId);
            videoStreamDeviceDetailMapper.saveRecord(vsdd);
            return new Msg(true);
        }
        return new Msg(false, "通话记录保存失败,设备不存在！");
    }
}
